<?php
////////////////////////////////////////////////////
//	Include all pages matched Regular Expression.
//	Customized by miao, based on include.inc.php.
////////////////////////////////////////////////////

// Default value of 'title|notitle' option
define('PLUGIN_INCLUDE_WITH_TITLE', TRUE);
define('PLUGIN_INCLUDE_MAX', 31);

function include_page($page, $root, $with_title)
{
	global $_msg_include_restrict, $script, $get, $post;
	static $count = 1;
	static $included = array();
	
	$s_page = htmlspecialchars($page);
	$r_page = rawurlencode($page);
	$link = '<a href="'.$script.'?'.$r_page.'">'.$s_page.'</a>';
	
	if (isset($included[$page])) {
		return '#include(): Included already: ' . $link . '<br />' . "\n";
	}
	if (! is_page($page)) {
		//return '#include(): No such page: ' . $s_page . '<br />' . "\n";
		$link = '<a href="'.$script.'?cmd=edit&amp;page='.$r_page.'">'.$s_page.'</a> EMPTY';
		return ''.$link.'<br>';
	} 
	if ($count > PLUGIN_INCLUDE_MAX) {
		return '#include(): Limit exceeded: '.$link.'<br/>'."\n";
	} 
	else {
		++$count;
	}
	// One page, only one time, at a time
	$included[$page] = TRUE;
	// Include A page, that probably includes another pages
	$get['page'] = $post['page'] = $vars['page'] = $page;
	if (check_readable($page, false, false)) {
		$body = convert_html(get_source($page));
	} 
	else {
		$body = str_replace('$1', $page, $_msg_include_restrict);
	}
	$get['page'] = $post['page'] = $vars['page'] = $root;
	
	// Put a title-with-edit-link, before including document
	if ($with_title) {
		$link = '<a href="'.$script.'?cmd=edit&amp;page='.$r_page.'">'.$s_page.'</a>';
		if ($page == $menubar) {
			$body = '<span align="center"><h5 class="side_label">'.$link.'</h5></span><small>'.$body.'</small>';
		} 
		else {
			$body = '<h2>'.$link.'</h2>'."\n".$body."\n";
		}
	}
	return $body;	
}


function plugin_include_re_convert()
{
	global $vars, $menubar;
	// $menubar will already be shown via menu plugin
	if (! isset($included[$menubar])) {
		$included[$menubar] = TRUE;
	}
	// Loop yourself
	$root = isset($vars['page']) ? $vars['page'] : '';
	$included[$root] = TRUE;
	// Get arguments
	$args = func_get_args();
	$with_title = PLUGIN_INCLUDE_WITH_TITLE;
	if (isset($args[0])) {
		switch(strtolower(array_shift($args))) {
			case 'title'  : $with_title = TRUE;  break;
			case 'notitle': $with_title = FALSE; break;
		}
	}
	// strip_bracket() is not necessary but compatible
	$pagename_re = strip_bracket(array_shift($args));
	//$dummy_ret .= $pagename_re."<br>";
	$filelist = scandir("./wiki/");
	$realnamelist = array();
	foreach($filelist as $filename) {
		//$dummy_ret .= $filename ."--->>";
		$realname = decode(str_replace(".txt", "", $filename));
		if(preg_match("/[.]+/", $realname)) {
			continue;
		}
		//$dummy_ret .= $realname ."<br>";
		//$dummy_ret .= $pagename_re."---".$realname."<br>";		
		if(preg_match($pagename_re, $realname)) {
			$full_realname = get_fullname($realname, $root);
			$realnamelist[] = $full_realname;
		}
	}
	$ret_html = "";
	foreach($realnamelist as $page) {
		$ret_html .= include_page($page, $root, $with_title);
	}
	return $ret_html;
}
?>
